package com.lzx.iteam;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.*;
import com.lzx.iteam.adapter.EventMemberAdapter;
import com.lzx.iteam.widget.gridimg.MyGridView;
import com.lzx.iteam.net.AsynHttpClient;
import com.lzx.iteam.net.EventMemberMsg;
import com.lzx.iteam.net.LocalLogin;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shijunting on 15-5-27.
 */
public class EventReceiveMemberActivity extends Activity implements View.OnClickListener {


    //private EventMemberAdapter mNormalAdapter;
    private EventMemberAdapter mReadedAdapter;
    private EventMemberAdapter mUnReadAdapter;
    //    private GridView mNormalGrid;
    private MyGridView mReadedGrid;
    private MyGridView mUnReadGrid;
    private GridView mNormalGrid;
    private TextView mReadedCount, mUnReadCount;
    private TextView mMemberCount;
    private LinearLayout mBack;
    private ScrollView mMemberForCreatorLayout;

    private String mEventId, mUserType, mEventType;
    private final int EVENT_MEMBER_MSG = 101;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case EVENT_MEMBER_MSG:
                    if (msg.arg1 == 0) {
                        Map<String, ArrayList<EventMemberMsg>> map = (HashMap<String, ArrayList<EventMemberMsg>>) msg.obj;
                        if (map != null) {
                            if (mEventType.equals("7")) {  // 活动
                                ArrayList<EventMemberMsg> unJoinList = map.get("unJoin");
                                ArrayList<EventMemberMsg> joinList = map.get("join");
                                mReadedCount.setText(joinList.size() + "人已报名");
                                mUnReadCount.setText(unJoinList.size() + "人未报名");
                                mReadedAdapter = new EventMemberAdapter(EventReceiveMemberActivity.this, joinList);
                                mUnReadAdapter = new EventMemberAdapter(EventReceiveMemberActivity.this, unJoinList);
                                mReadedGrid.setAdapter(mReadedAdapter);
                                mUnReadGrid.setAdapter(mUnReadAdapter);
                            } else if (mEventType.equals("1")) {  // 公告
                                ArrayList<EventMemberMsg> unReadList = map.get("unRead");
                                ArrayList<EventMemberMsg> readedList = map.get("readed");
                                mReadedCount.setText(readedList.size() + "人已读");
                                mUnReadCount.setText(unReadList.size() + "人未读");
                                mReadedAdapter = new EventMemberAdapter(EventReceiveMemberActivity.this, readedList);
                                mUnReadAdapter = new EventMemberAdapter(EventReceiveMemberActivity.this, unReadList);
                                if (unReadList.size() == 0) {
                                    mUnReadAdapter.bindData("暂无");
                                }
                                mReadedGrid.setAdapter(mReadedAdapter);
                                mUnReadGrid.setAdapter(mUnReadAdapter);

                                /*if (unReadList.size() ==0){
                                    mMemberCount.setText("全部已读");
                                    //mNormalGrid.setVisibility(View.GONE);
                                }else{
                                    mUnReadAdapter = new EventMemberAdapter(EventReceiveMemberActivity.this, unReadList);
                                    mMemberCount.setText(readedList.size() + "人已读," + unReadList.size() + "人未读");
                                    mNormalGrid.setAdapter(mUnReadAdapter);
                                    mNormalGrid.setVisibility(View.VISIBLE);
                                }*/
                            } else if (mEventType.equals("8")) {  // 投票
                                ArrayList<EventMemberMsg> unReadList = map.get("unVote");
                                ArrayList<EventMemberMsg> readedList = map.get("vote");
                                mReadedCount.setText(readedList.size() + "人已投票");
                                mUnReadCount.setText(unReadList.size() + "人未投票");
                                mReadedAdapter = new EventMemberAdapter(EventReceiveMemberActivity.this, readedList);
                                mUnReadAdapter = new EventMemberAdapter(EventReceiveMemberActivity.this, unReadList);
                                if (unReadList.size() == 0) {
                                    mUnReadAdapter.bindData("暂无");
                                }
                                mReadedGrid.setAdapter(mReadedAdapter);
                                mUnReadGrid.setAdapter(mUnReadAdapter);

                            } else if (mEventType.equals("9")) {  // 签到
                                ArrayList<EventMemberMsg> unJoinList = map.get("unJoin");
                                ArrayList<EventMemberMsg> joinList = map.get("join");
                                mReadedCount.setText(joinList.size() + "人已签到");
                                mUnReadCount.setText(unJoinList.size() + "人未签到");
                                mReadedAdapter = new EventMemberAdapter(EventReceiveMemberActivity.this, joinList);
                                mUnReadAdapter = new EventMemberAdapter(EventReceiveMemberActivity.this, unJoinList);
                                if (unJoinList.size() == 0) {
                                    mUnReadAdapter.bindData("暂无");
                                }
                                mReadedGrid.setAdapter(mReadedAdapter);
                                mUnReadGrid.setAdapter(mUnReadAdapter);
                            } else {  // 普通事件 + 没有开启二维码的会议通知
                                ArrayList<EventMemberMsg> unReadList = map.get("unRead");
                                ArrayList<EventMemberMsg> readedList = map.get("readed");
                                mReadedCount.setText(readedList.size() + "人已读");
                                mUnReadCount.setText(unReadList.size() + "人未读");
                                mReadedAdapter = new EventMemberAdapter(EventReceiveMemberActivity.this, readedList);
                                mUnReadAdapter = new EventMemberAdapter(EventReceiveMemberActivity.this, unReadList);
                                if (!mEventType.equals("9.5")){
                                    mUnReadAdapter.bindData("添加");
                                }
                                mReadedGrid.setAdapter(mReadedAdapter);
                                mUnReadGrid.setAdapter(mUnReadAdapter);
                            }

                        }
                        /*else {
                            ArrayList<EventMemberMsg> eventMemberMsgs = (ArrayList<EventMemberMsg>) msg.obj;
                            if (eventMemberMsgs != null) {
                                mNormalAdapter = new EventMemberAdapter(EventReceiveMemberActivity.this, eventMemberMsgs);
                                mNormalGrid.setAdapter(mNormalAdapter);
                            }
                        }*/
                    } else {
                        if (msg.arg1 == AsynHttpClient.ERROR_LOCAL) {
                            Toast.makeText(EventReceiveMemberActivity.this, R.string.active_failure_no_network,
                                    Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(EventReceiveMemberActivity.this, (String) msg.obj, Toast.LENGTH_LONG).show();
                        }
                    }
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.event_receive_member_layout);
        initViews();
        getIntentData();
        getEventMember();
    }


    private void initViews() {
        mBack = (LinearLayout) findViewById(R.id.event_member_back);
        mNormalGrid = (GridView) findViewById(R.id.event_member_for_user);
        mMemberForCreatorLayout = (ScrollView) findViewById(R.id.event_member_for_creator);
        mReadedGrid = (MyGridView) findViewById(R.id.event_readed_member);
        mUnReadGrid = (MyGridView) findViewById(R.id.event_unread_member);
        mReadedCount = (TextView) findViewById(R.id.event_readed_count);
        mUnReadCount = (TextView) findViewById(R.id.event_unread_count);
        mMemberCount = (TextView) findViewById(R.id.event_member_count);
        mBack.setOnClickListener(this);
        mUnReadGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if ("添加".equals(((EventMemberMsg) mUnReadAdapter.getItem(i)).getUserName())) {
                    Intent intent = new Intent(EventReceiveMemberActivity.this, ChooseContactByTeamActivity.class);
                    intent.setAction("event_add_receiver");
                    intent.putExtra("event_id", mEventId);
                    intent.putExtra("event_type", mEventType);
                    startActivity(intent);
                }

            }
        });
    }

    private void getIntentData() {
        Intent intent = getIntent();
        mEventId = intent.getStringExtra("event_id");
        mUserType = "1";
        mEventType = intent.getStringExtra("event_type");

        /*if (mEventType.equals("7") || mEventType.equals("8")  ){

            mMemberCount.setVisibility(View.GONE);
            mNormalGrid.setVisibility(View.GONE);
            mMemberForCreatorLayout.setVisibility(View.VISIBLE);
        }else{
            
            mMemberCount.setVisibility(View.VISIBLE);
            mMemberForCreatorLayout.setVisibility(View.GONE);
        }*/
    }

    /**
     * 获取事件成员
     */
    public void getEventMember() {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
                .getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_EVENT_ID, mEventId));
        EventMemberMsg message = new EventMemberMsg(mHandler.obtainMessage(EVENT_MEMBER_MSG), mEventType);
        message.mApi = AsynHttpClient.API_EVENT_MEMBER;
        message.mParams = nvps;
        AsynHttpClient.getInstance(this).execEventHttp(message);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.event_member_back:
                finish();
        }
    }
}
